package com.api.gbhg.daiyanlu.statement;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.BudgetaryPerformanceDao;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.liwenjun.GBHGConstant;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.google.common.base.Strings;
import weaver.conn.RecordSet;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 *预算执行情况表:预算年度,单位,预算项目,预算总额,审批中费用,实际发生数,可用额度,执行率(%)
 * 查询条件:年度,单位,项目
 * Mr.dyl
 */
@Path("/bp")
public class BudgetaryPerformance {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    BudgetaryPerformanceDao dao=new BudgetaryPerformanceDao();
    @POST
    @Path("/getBp")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBp(Map<String,Object> param) {
        String xm = getProjectNameReturnType((String) param.get("xm"));
        if(!Strings.isNullOrEmpty(xm)){
            param.put("xm",xm);
        }
        Integer pageNum = 1;//当前页面
        Integer pageSize = 5;//显示数量
        if (param.containsKey("pageNum") && param.get("pageNum") instanceof Integer && (Integer) param.get("pageNum") > 0)
            pageNum = (Integer) param.get("pageNum");
        if (param.containsKey("pageSize") && param.get("pageSize") instanceof Integer && (Integer) param.get("pageSize") > 0)
            pageSize = (Integer) param.get("pageSize");
        int nd = getNd(param);
        String dw = "2";
        if (param.containsKey("dw") && param.get("dw") instanceof Integer) {
            dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }
        RecordSet rs = dao.getBpDao(pageNum, pageSize,nd,dw,xm);
        Map<String, Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<String, Object>();
            map[i].put("ysqj",rs.getString("ysnd"));//预算期间
            RecordSet sbdwRs = rmcd.getSBDWById(rs.getInt("sbdw"));
            sbdwRs.next();
            map[i].put("ysdw", sbdwRs.getString("SUBCOMPANYNAME"));//预算单位
            map[i].put("ysxm",rs.getString("xmmin"));//预算项目
            map[i].put("yszed",rs.getString("yszje"));//预算总额度
            map[i].put("spzfy",rs.getString("dj"));//审批中费用
            map[i].put("sjfss", FormatUtil.DmFormat((rs.getDouble("yszje") - rs.getDouble("syed"))));//实际发生数
            map[i].put("kyed",rs.getString("syed"));//可用额度
            map[i].put("zxb", AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("yszje")));//执行比
            i++;
        }
        RecordSet es=dao.getBpMax(nd,dw,xm);
        BigDecimal bd7 = new BigDecimal("100");
        BigDecimal bd8 = new BigDecimal("1");
        BigDecimal yszed=new BigDecimal("0.00");//预算总额合计
        BigDecimal spzfy=new BigDecimal("0.00");//审批中费用合计
        BigDecimal kyed=new BigDecimal("0.00");//可用额度合计
        BigDecimal sjfss=new BigDecimal("0.00");//实际发生数合计
        BigDecimal zxb=new BigDecimal("0.00");//执行率合计
        while (es.next()){
            yszed=new BigDecimal(es.getString("yszje"));
            spzfy=new BigDecimal(es.getString("dj"));
            kyed=new BigDecimal(es.getString("syed"));
            //sjfss=sjfss.add()new BigDecimal(FormatUtil.DmFormat((rs.getDouble("yszje") - rs.getDouble("syed"))));
            //zxl=new BigDecimal(AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("yszje")));
        }
        sjfss=yszed.divide(bd8,2,BigDecimal.ROUND_HALF_UP).subtract(kyed);
        zxb=kyed.divide(yszed,2,BigDecimal.ROUND_HALF_UP).multiply(bd7);
        com.api.gbhg.daiyanlu.DataPrintCopyUtil.Info<String> pageInfo = new com.api.gbhg.daiyanlu.DataPrintCopyUtil.Info<>();
        int num = dao.getBpCount(nd,dw,xm);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(num);
        pageInfo.setZyszed(yszed);
        pageInfo.setZspzfy(spzfy);
        pageInfo.setZkyed(kyed);
        pageInfo.setZsjfss(sjfss);
        pageInfo.setZzxb(zxb);
        pageInfo.setList(map);
        return JSON.toJSONString(pageInfo);
    }

    public int getNd(Map<String,Object> param){
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }

    public static String getProjectNameReturnType(String xm){
        String type=null;
        if(!Strings.isNullOrEmpty(xm)){
            GBHGConstant.FlowVersionTableEnum[] values = GBHGConstant.FlowVersionTableEnum.values();
            for (int i = 0; i < values.length; i++) {
                String flowName = values[i].getFlowName();
                System.out.println(flowName.substring(3,flowName.length()-1));
                if(xm.equals(flowName.substring(3,flowName.length()-1))){
                    type=values[i].getFlowType();
                }
            }
        }
        return type;
    }
}
